/*
 * @Author: 晟松
 * @Date: 2021-11-29 23:48:04
 * @LastEditors: 晟松
 * @LastEditTime: 2022-01-22 21:21:00
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\app.js
 * @Description:
 */
// app.js
import "dotenv/config";
import "module-alias/register";
// dotenv是为了能访问 .env文件里的数据
// module-alias/register是能使用@main的别名
const Koa = require("koa");
import router from "@main/router";
import middleware from "@main/middleware";
import validateParams from "@main/middleware/routeMid/validateParams";
import { enumsToTrans } from "@main/util/EnumsUtil";

const app = new Koa();

// 统一JSON返回格式,同时对所有的enums值进行统一转换变为字符串
app.context.renderJson = (data, msg = "操作成功", code = 200) => {
    enumsToTrans(data)
    return {
        code,
        data,
        msg,
    };
};
// 验证参数
app.context.validateParams = validateParams;

// 中间件处理
middleware(app);

// 加载所有路由
router(app);

module.exports = app;
